<template>
	<view style="display: flex; flex-direction: column; width: 100%; height: 100%;">
		<view v-if="!adreaded" style="margin: 100px 30px auto 30px;">
			<uv-button @click="showVideoAd" type="success">{{ isVideoAdLoaded ? "看广告" : "广告加载中" }}</uv-button>
		</view>
		<view v-if="adreaded" style="display: flex;flex-direction: column; margin: 100px 30px auto 30px;">
			<text style="text-align: center; justify-content: center;">广告观看完成,请返回</text>
			<text
				style="text-align: center; justify-content: center; margin: 10px auto; font-size: 0.7rem; color: blue;"
				@click="showVideoAd">继续观看广告>></text>
			<uv-button type="success" style="margin: auto; width: 300rpx;" @click="gotohome">前往资源首页>></uv-button>
		</view>
		<view style="flex-grow: 1;flex-flow: 1;"></view>
		<view style="margin: 20rpx 10rpx 20rpx 10rpx">
			<ad v-if="showad" :unit-id="config.FullVideoID" ad-type="video" ad-theme="white"></ad>
		</view>
		<uv-toast ref="toast"></uv-toast>
	</view>
</template>

<script setup>
	import {
		onMounted,
		ref
	} from 'vue';
	import {
		onShow,
		onLoad
	} from '@dcloudio/uni-app'
	import config from '../../common/config.js';
	let interstitialAd = null;
	let isInterstitialLoading = false;
	let rewardedVideoAd = null;
	let isVideoAdLoaded = ref(false);
	let modal = ref(null)
	let toast = ref(null)
	let looked = false;
	let query = null
	let showad = ref(false)
	let adreaded = ref(false)

	function adconfirm() {
		showVideoAd();
	}

	function adcancel() {

	}


	onLoad((op) => {
		if (op.query != null) {
			let url = op.query
			url = decodeURIComponent(url)
			url = decodeURIComponent(url)
			query = JSON.parse(url)
			console.log(query)
		}
		showad.value = true
	})

	onShow(() => {
		//激励广告
		if (wx.createRewardedVideoAd) {
			rewardedVideoAd = wx.createRewardedVideoAd({
				adUnitId: config.RewardedVideoID
			})
			rewardedVideoAd.onLoad(() => {
				console.log('激励广告加载成功');
				isVideoAdLoaded.value = true
			})
			rewardedVideoAd.onError((e) => {
				console.error('激励视频广告加载失败', e)
				showToast({
					type: 'default',
					message: e,
				})
			})
			rewardedVideoAd.onClose((e) => {
				// 用户点击了关闭广告
				isVideoAdLoaded.value = false;
				console.log("激励广告关闭", e);

				uni.request({
					url: query.callback,
					data: {
						password: config.adPassword,
						type: query.type,
						userid: query.userid,
						id: query.id,
						value: ((e && e.isEnded) || adreaded.value) ? 1 : 2
					},
					success: (res) => {
						console.log(res, e)
						let data = res.data
						if (data.data && data.data.result) {
							if (e && e.isEnded) {
								adreaded.value = true
								toast.value.show({
									message: "广告观看完成"
								})
							} else {
								toast.value.show({
									message: "广告未观看完成"
								})
							}
						} else {
							toast.value.show({
								message: "上传结果错误"
							})
						}
					},
					fail: (err) => {
						toast.value.show({
							message: "服务器出错"
						})
					}
				})
			})
		}
	})

	function showVideoAd() {
		if (isVideoAdLoaded.value) {
			rewardedVideoAd.show();
		} else {
			showToast({
				type: 'default',
				title: '激励广告未加载完成',
				message: "激励广告未加载完成",
			})
		}
	}

	function gotohome() {
		uni.redirectTo({
			url: "/pages/index/index"
		})
	}
</script>

<style>
	page {
		display: flex;
		flex-direction: column;
		width: 100%;
		height: 100%;
	}
</style>